<?php
/**
 * 主框架
 * @author sundongdong
 * @date 2016-07-05
 */
class IndexController extends Controller_NeedLogin {
	/**
	 * 初始化父类构造器
	 * {@inheritDoc}
	 * @see Zend_Controller_Action::init()
	 */
	public function init() {
		parent::init();
	}
	/**
	 * @desc IFS 系统主界面
	 */
	public function indexAction() {
		$model = new Application_Model_Privilege();
		// 获取当前用户的权限列表
		$permit_menus = $model->getUserPrivilege($this->_session->user_id);
		// 获取顶层菜单
		$topMenus = $model->getMenuByPid(0);
		$secondMenus = array();
		foreach ($topMenus as $topMenuKey => $topMenuValue) {
			// 判断权限 和是否显示
			if ($topMenuValue['is_show'] == 0 || !in_array($topMenuValue['menu_id'], $permit_menus)) {
				unset($topMenus[$topMenuKey]);
				continue;
			}
			$secondMenus[$topMenuKey] = $model->getMenuByPid($topMenuValue['menu_id']);
			foreach ($secondMenus[$topMenuKey] as $secondMenuKey => $secondMenuValue) {
				// 判断权限 和是否显示
				if ($secondMenuValue['is_show'] == 0 || !in_array($secondMenuValue['menu_id'], $permit_menus)) {
					unset($secondMenus[$topMenuKey][$secondMenuKey]);
					continue;
				}
				$children = $model->getMenuByPid($secondMenuValue['menu_id']);
				foreach ($children as $childKey => $childValue) {
					// 判断权限 和是否显示
					if ($childValue['is_show'] == 0 || !in_array($childValue['menu_id'], $permit_menus)) {
						unset($children[$childKey]);
						continue;
					}
				}
				if (count($children) > 0) {
					$secondMenus[$topMenuKey][$secondMenuKey]['children'] = $children;
				}
			}
		}
		//获取自定义菜单id
		$customMenuId = $model->getCustomMenuId($this->_session->user_id);

		$customMenu = array();
		if (!empty($customMenuId)) {
			//获取自定义菜单
			$customMenu = $model->getCustomMenu($customMenuId);
		}
		$menuID = array_column($customMenuId, 'menu_id');

		$this->smarty->assign('username', $this->_session->real_name);
		$this->smarty->assign('topMenus', $topMenus);
		$this->smarty->assign('subMenus', $secondMenus);
		$this->smarty->assign('customMenuID', $menuID);
		$this->smarty->assign('customMenu', $customMenu);
		$this->smarty->display('index.tpl');
	}
	/**
	 * @desc IFS 系统首页
	 */
	public function mainAction() {
		$this->smarty->display('welcome.tpl');
	}
	/**
	 * 常用菜单更新
	 */
	public function custommenusAction() {
		$getData = $this->getRequest()->getParams();
		$model = new Application_Model_Privilege();
		$res = $model->updateCustomMenu($getData['strValue'], $this->_session->user_id);
		echo json_encode($res);
	}
	/**
	 * 退出登录
	 */
	public function loginoutAction() {
		Zend_Session::destroy();

		$returnUrl = "http://" . $_SERVER['HTTP_HOST'];
		$strurli = base64_encode($returnUrl);
		$ssoHost = Helper_Config::get('base', 'sso', 'host');
		$url = 'Location: ' . $ssoHost . '/login/index/loginout/?struli=' . $strurli . '&from=IFS';

		header($url);
	}
}